package com.naiterui.ehp.bs.patient.controller.inner;

import com.naiterui.ehp.bp.bo.emr.AddPatientRecordInitializeBO;
import com.naiterui.ehp.bp.bo.patient.PatientInquirerBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientInquirerService;
import com.naiterui.ehp.bs.patient.service.IPatientService;
import com.naiterui.ehp.bs.patient.vo.inquirer.InquirerRelationVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @Description 就诊人相关内部接口
 * @Author gongbaoqiang
 * @Date 2020/7/3 7:14 PM
 */
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.USER_INFO})
@Slf4j
@RestController
@RequestMapping("ap/inner/patient/inquirer")
public class ApInnerPatientInquirerController {

    @Autowired
    private IPatientInquirerService inquirerService;
    @Autowired
    private IPatientService patientService;

    /**
     * 就诊人列表
     *
     * @return
     */
    @PostMapping("/list")
    public ResponseVO<List<PatientInquirerBO>> list(@RequestBody List<Long> inquirerIds) throws BusinessException {
        log.info("就诊人列表，参数信息 inquirerIds={}", inquirerIds.toString());

        return PublicService.returnResponseVO(inquirerService.findByIds(inquirerIds));
    }

    /**
     * 就诊人列表
     *
     * @return
     */
    @PostMapping("/list/{patientId}")
    public ResponseVO<List<PatientInquirerBO>> list(@PathVariable Long patientId) throws BusinessException {
        log.info("就诊人列表，参数信息 patientId={}", patientId.toString());

        return PublicService.returnResponseVO(inquirerService.findByPatient(patientId));
    }

    /**
     * 按就诊人关系查询就诊人
     *
     * @return
     */
    @PostMapping("/list/by/relation")
    public ResponseVO<List<PatientInquirerBO>> listByRelation(Long patientId, Integer relation) throws BusinessException {
        log.info("按就诊人关系查询就诊人，参数信息 patientId={}， relation={}", patientId, relation);

        return PublicService.returnResponseVO(inquirerService.findByPatientAndRelation(patientId, relation));
    }

    /**
     * 就诊人关系列表
     *
     * @return
     */
    @PostMapping("/relation/{patientId}")
    public ResponseVO<List<InquirerRelationVO>> relation(@PathVariable Long patientId) throws BusinessException {
        log.info("就诊人关系列表，参数信息 patientId={}", patientId);

        return PublicService.returnResponseVO(inquirerService.findRelationByPatientId(patientId));
    }

    /**
     * 添加病历初始化带入病情信息
     *
     * @param sessionId
     * @return
     */
    @GetMapping("record/initialize")
    public ResponseVO<AddPatientRecordInitializeBO> getAddRecordInitializerInfo(Long inquirerId, String sessionId) {
        return PublicService.returnResponseVO(patientService.getAddRecordInitializerInfo(inquirerId, sessionId));
    }

    /**
     * 获取患者本人就诊人
     *
     * @param patientId
     * @return
     */
    @GetMapping("myself")
    public ResponseVO<Long> getMyselfByPatientId(@RequestParam("patientId") Long patientId) {
        return PublicService.returnResponseVO(inquirerService.findMyselfByPatientId(patientId));
    }

    /**
     * 完善就诊人本人信息
     *
     * @param patientId
     * @param nickName
     * @return
     */
    @GetMapping("supplement/myself/info")
    public ResponseVO<Void> supplementMyselfInfo(Long patientId, String nickName) {
        inquirerService.supplementMyselfInfo(patientId, nickName);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 获取默认就诊人
     *
     * @return
     */
    @PostMapping("/default/{patientId}")
    public ResponseVO<PatientInquirerBO> getDefultInquirerInfoByPatientId(@PathVariable Long patientId,
                                                                          @RequestParam(value = "doctorId", required = false) Long doctorId) {
        log.info("获取默认就诊人参数信息 patientId={}, doctorId={}", patientId, doctorId);
        return PublicService.returnResponseVO(inquirerService.getDefaultInquirerInfoByPatientId(doctorId, patientId));
    }

    /**
     * 根据就诊人Id查询就诊人信息
     *
     * @return
     */
    @GetMapping("/{inquirerId}")
    public ResponseVO<PatientInquirerBO> getInquirerInfoById(@PathVariable Long inquirerId) {
        log.info("根据就诊人Id查询就诊人信息 inquirerId={}", inquirerId);
        return PublicService.returnResponseVO(inquirerService.getInquirerInfoById(inquirerId));
    }

    /**
     * 根据身份证号查询就诊人id列表
     *
     * @param idcard
     * @return
     */
    @GetMapping("/inquirerIds")
    public ResponseVO<List<Long>> getInquirerIdsByIdcard(@NotEmpty(message = "身份证号不能为空") String idcard) {
        log.info("根据身份证号查询就诊人id列表，参数信息 idcard={}", idcard);
        return PublicService.returnResponseVO(inquirerService.getInquirerIdsByIdcard(idcard));
    }
}
